package instruments;

import data.NavmeshRegion;
import data.InstrumentGraphics;
import data.Params;
import java.awt.Color;
import java.awt.Point;
import java.util.List;
import pathfind.LocalPathFinder;
import pathfind.PathSmoother;

/**
 * @author sad
 */
public class LocalFinderInstrument extends AbstractPolygonInstrument {
    private Point p1;
    private Point p2;
    private NavmeshRegion polygon;
    private List<Point> path;

    @Override
    public void mouseDown(int x, int y, int modifiers) {
        super.mouseDown(x, y, modifiers);
        NavmeshRegion p = trySelect(x, y);
        if (p == null) {
            return;
        }

        if (p != polygon) {
            p1 = null;
            p2 = null;
            path = null;
        }

        polygon = p;
        Point point = new Point();
        polygon.getIndexOfCell(x, y, point);
        if (isLeftDown(modifiers)) {
            p1 = point;
        }

        if (isRightDown(modifiers)) {
            p2 = point;
        }

        if (p1 != null && p2 != null) {
            long mill = System.currentTimeMillis();
            path = recalculatePath();
            long mill2 = System.currentTimeMillis();
            System.out.println("Path = " + (mill2 - mill));
            mill = System.currentTimeMillis();
            if (path != null) {
                PathSmoother smoother = new PathSmoother(polygon, path);
                path = smoother.smooth();
            }
            long mill3 = System.currentTimeMillis();
            System.out.println("Smooth = " + (mill3 - mill));
        }
        redraw();
    }

    protected List<Point> recalculatePath() {
        LocalPathFinder finder = new LocalPathFinder(p2, polygon);
        List<Point> tpath = finder.computeWithFallback(p1);
        return tpath;
    }

    @Override
    public void onDraw(InstrumentGraphics g) {
        super.onDraw(g);
        if (polygon != null) {
            if (path != null && !path.isEmpty()) {
                g.setColor(Color.YELLOW);
                for (int i = 0; i < path.size() - 1; i++) {
                    double x1 = polygon.getX() + path.get(i).x * Params.CELL_SIZE + Params.CELL_SIZE / 2;
                    double y1 = polygon.getY() + path.get(i).y * Params.CELL_SIZE + Params.CELL_SIZE / 2;
                    double x2 = polygon.getX() + path.get(i + 1).x * Params.CELL_SIZE + Params.CELL_SIZE / 2;
                    double y2 = polygon.getY() + path.get(i + 1).y * Params.CELL_SIZE + Params.CELL_SIZE / 2;
                    g.drawLine(x1, y1, x2, y2);
                }
            }

            if (p1 != null) {
                g.setColor(Color.red);
                double x = polygon.getX() + p1.x * Params.CELL_SIZE;
                double y = polygon.getY() + p1.y * Params.CELL_SIZE;
                g.fillRect(x, y, Params.CELL_SIZE, Params.CELL_SIZE);
            }

            if (p2 != null) {
                g.setColor(Color.CYAN);
                double x = polygon.getX() + p2.x * Params.CELL_SIZE;
                double y = polygon.getY() + p2.y * Params.CELL_SIZE;
                g.fillRect(x, y, Params.CELL_SIZE, Params.CELL_SIZE);
            }
        }
    }
}
